package com.ax.base.drugproducer.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ax.base.common.domain.Tree;
import com.ax.base.common.utils.AjaxRes;
import com.ax.base.common.utils.Const;
import com.ax.base.common.utils.PageUtils;
import com.ax.base.drugproducer.domain.BaseDrug;
import com.ax.base.drugproducer.service.DrugService;
import com.ax.base.system.domain.MenuDO;

@Controller
@RequestMapping("/sys/drug")
public class DrugController {
   
	@Autowired
	DrugService drugService;
	
	/**
	 * 菜单页面的跳转
	 */
	@GetMapping()
	String drugAdministration(Model model) {
		return "drugproducer/drug/drug";
	}
	
	/**
	 * 药品查询(模块:药品)*包含药品的药品分类信息
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return PageUtils
	 */
	@RequestMapping("/queryDrug")
	@ResponseBody
	public List<BaseDrug> queryDrug(@RequestParam Map<String,Object> params) {
	//	PageUtils drugs = this.drugService.queryDrugSer(params);
		List<BaseDrug> BaseDrugs = this.drugService.queryDrugSer(params);
		return BaseDrugs;
	  }
	
	/**
	 * 药品增加(模块:药品)*跳转增加页面
	 * 
	 * @param 无
	 *           
	 * @return HTML
	 */
	@GetMapping("/add")
	String add(Model model) {
		return "drugproducer/drug/add";
	}
	
	/**
	 * 药品保存(模块:药品)
	 * 
	 * @param BaseDrug baseDrug
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/insertBaseDrug")
	@ResponseBody
	public AjaxRes insertBaseDrug(AjaxRes ajaxRes,BaseDrug baseDrug){
		try {
			this.drugService.insertBaseDrugSer(baseDrug);
			ajaxRes.setSucceed(Const.SAVE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * 药品修改(模块:药品)*跳转修改页面
	 * 
	 * @param Long id
	 *           
	 * @return HTML
	 */
	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		model.addAttribute("drug",drugService.getDrugSer(id));
		return "drugproducer/drug/edit";
	}
	
	/**
	 * 药品修改(模块:药品)
	 * 
	 * @param BaseDrug baseDrug
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/updateBaseDrug")
	@ResponseBody
	public AjaxRes updateDrugManager(AjaxRes ajaxRes,BaseDrug baseDrug){
		try {
			this.drugService.updateBaseDrugSer(baseDrug);
			ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * Tree
	 * 
	 * @param 无
	 *           
	 * @return Tree<MenuDO>
	 */
	@GetMapping("/tree")
	@ResponseBody
	Tree<MenuDO> tree() {
		Tree<MenuDO> tree = drugService.getTree();
		return tree;
	}
	
	/**
	 * 药品端树形选择Tree
	 * 
	 * @param String drugClassCode
	 *           
	 * @return Tree<MenuDO>
	 */
	@GetMapping("/baseDrugTree")
	@ResponseBody
	Tree<MenuDO> getbaseDrugOtherTree(String drugClassCode) {
		Tree<MenuDO> tree = drugService.getbaseDrugOtherTree(drugClassCode);
		return tree;
	}
}
